package top.huahaizhi.zhihu.daily.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import butterknife.BindView;
import top.huahaizhi.zhihu.daily.BaseActivity;
import top.huahaizhi.zhihu.daily.K;
import top.huahaizhi.zhihu.daily.R;
import top.huahaizhi.zhihu.daily.bean.MainSplashBean;
import top.huahaizhi.zhihu.daily.util.NetUtils;
import top.huahaizhi.zhihu.daily.util.Utils;

public class SplashActivity extends BaseActivity {

    @BindView(R.id.imageView_main_bg)
    ImageView mBgView;
    @BindView(R.id.tv_splash_copyright)
    TextView copyRightView;

    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_splash;
    }

    @Override
    protected void setParams() {
        super.setParams();
        NetUtils.getInstance(this).go(K.SPLASH_BG, new NetUtils.OnRequestListener<String>() {
            @Override
            public void onSuccess(String object) {
                MainSplashBean mainSplashBean = new Gson().fromJson(object, MainSplashBean.class);
                final MainSplashBean.ImagesBean image = mainSplashBean.getImages().get(0);
                String imgUrl = "http://www.bing.com" + image.getUrl();
                String imgCopyRight[] = image.getCopyright().split("\\(");
                Utils.getInstance(SplashActivity.this).loadImage(imgUrl, mBgView,-1,R.drawable.splash);
                copyRightView.setText(imgCopyRight[0]+"\n("+imgCopyRight[1]);
                copyRightView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent viewIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(image.getCopyrightlink()));
                        startActivity(viewIntent);
                    }
                });
            }

            @Override
            public void onFail(String reason) {

            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this,ContentActivity.class));
                finish();
            }
        },3000);
    }

    @Override
    public void onBackPressed() {}
}
